﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace ChartSharp.QR
{
    public class QRCode : BaseChart
    {
        private const EncodingType DefaultEncodingType = EncodingType.UTF8;
        private const ErrorCorrectionLevel DefaultErrorCorrectionLevel = ErrorCorrectionLevel.L;
        private const int DefaultMargin = QROptions.MinimalMargin;


        protected ChartParam _data;
        protected EncodingTypeParam _encoding;
        protected QROptions _options;

        public QRCode(int width, int height)
            : base(GetQRCodeCharType(), new Size(width, height))
        {
            _encoding = new EncodingTypeParam(DefaultEncodingType);
            _options = new QROptions(DefaultErrorCorrectionLevel, DefaultMargin);
        }

        public QRCode(Size size)
            : base(GetQRCodeCharType(), size)
        {
            _encoding = new EncodingTypeParam(DefaultEncodingType);
            _options = new QROptions(DefaultErrorCorrectionLevel, DefaultMargin);
        }

        public QRCode(Size size, string data)
            : base(GetQRCodeCharType(), size)
        {
            _data = new PlainParam("chl", HttpUtility.UrlPathEncode(data));
            _encoding = new EncodingTypeParam(DefaultEncodingType);
            _options = new QROptions(DefaultErrorCorrectionLevel, DefaultMargin);
        }

        public QRCode(Size size, string data, EncodingType encoding) : base(GetQRCodeCharType(), size)
        {
            _data = new PlainParam("chl", HttpUtility.UrlPathEncode(data));
            _encoding = new EncodingTypeParam(encoding);
            _options = new QROptions(DefaultErrorCorrectionLevel, DefaultMargin);
        }

        public QRCode(Size size, string data, EncodingType encoding, ErrorCorrectionLevel level)
            : base(GetQRCodeCharType(), size)
        {
            _data = new PlainParam("chl", HttpUtility.UrlPathEncode(data));
            _encoding = new EncodingTypeParam(encoding);
            _options = new QROptions(level, DefaultMargin);
        }

        public QRCode(Size size, string data, EncodingType encoding, ErrorCorrectionLevel level, int margin)
            : base(GetQRCodeCharType(), size)
        {
            _data = new PlainParam("chl", HttpUtility.UrlPathEncode(data));
            _encoding = new EncodingTypeParam(encoding);
            _options = new QROptions(level, margin);
        }

        public QRCode Text(string text)
        {
            _data = new PlainParam("chl", HttpUtility.UrlPathEncode(text));
            return this;
        }

        public QRCode Encoding(EncodingType encoding)
        {
            _encoding = new EncodingTypeParam(encoding);
            return this;
        }

        public QRCode Options(ErrorCorrectionLevel level, int margin)
        {
            _options = new QROptions(level, margin);
            return this;
        }

        private static Type GetQRCodeCharType()
        {
            return new Type("qr");
        }
    }
}
